//
//  StatusCellTopView.swift
//  weibo
//
//  Created by apple on 15/12/19.
//  Copyright © 2015年 LYR. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class StatusCellTopView: UIView {

    //定义 顶部视图的底部约束
    var bottomConstraints: Constraint?
    
    //MARK:定义外部模型属性
    var status: Status? {
     
        didSet {
            //头像
          iconImage.sd_setImageWithURL(status?.user?.headImageURL)
           
            //名称
            nameLable.text = status?.user?.name
            
            //会员图标
            mbrankImage.image = status?.user?.mbrankImage
           
            //  大V
            verified_type_image.image = status?.user?.verified_type_image
           
            // 发布时间
           // timeLable.text = status?.created_at
            
            // 来源
            //sourceLable.text = status?.source
            
            // 内容
            contentLable.text = status?.text
            
            // 设置配图视图的图片数据源
            pictureView.imageURLs = status?.imageURLs
            
            //在更新约束之前 将之前的约束关系 先卸载掉 底部的约束
            //更新的约束: (bottom)需要约束的属性 是一样  被参照的属性 也是一样的才能够进行更新操作
            self.bottomConstraints?.uninstall()
            //  是否有图片  调整约束关系
            if let urls = status?.imageURLs where urls.count > 0 {
               //  有配图
            
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }else {
              // 没有配图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(contentLable.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            }
            
            
        }
    }
    
    
    //MARK:重写构造方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
//        backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:设置页面 布局
    private func setupUI() {
        // 分割线视图
        addSubview(sepView)
        sepView.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加头像视图
        addSubview(iconImage)
        //添加姓名
        addSubview(nameLable)
        addSubview(mbrankImage)
        addSubview(verified_type_image)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(contentLable)
        
        addSubview(pictureView)
        
        // 添加分割线的约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(10)
        }
        
        //添加头像的约束
        iconImage.snp_makeConstraints { (make) -> Void in
            
            make.left.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            
            make.width.height.equalTo(CGSize(width: StatusHeadImageWidth, height: StatusHeadImageWidth))
        }
        
        //添加姓名的约束
        nameLable.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
            make.top.equalTo(iconImage.snp_top)
        }
        
        //会员图标
        mbrankImage.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(nameLable.snp_top)
            make.left.equalTo(nameLable.snp_right).offset(StatusCellMargin)
        }
        
        // V 认证
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        
        //发布时间
        timeLable.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconImage.snp_bottom)
        }
        
       //来源
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLable.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconImage.snp_bottom)
        }
        
        //内容
        contentLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            //make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        
        // 图片内容的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLable.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLable.snp_left)
            //make.size.equalTo(CGSize(width: 100, height: 100))
            
        }

        
        
        //  对顶部视图的底部进行约束
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
    }
    
    // MARK:懒加载所有的子视图
    //加载头像视图
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var nameLable = UILabel(title: "哈哈哈哈", color: themeColor, fontSize: 14)
    
    private lazy var mbrankImage = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    private lazy var verified_type_image = UIImageView (image: UIImage(named: "avatar_vip"))
    
    private lazy var timeLable = UILabel(title: "11:11", color: themeColor, fontSize: 10)
    
    private lazy var sourceLable = UILabel(title: "iPhone88", color: themeColor, fontSize: 10)
    
    private lazy var contentLable = UILabel(title:  "下雨天炸鸡和辣条更加配哦,下雨天炸鸡和辣条更加配哦下雨天炸鸡和辣条更加", color: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMargin)
    
    //配图视图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
    //分割线视图
    private lazy var sepView: UIView = UIView()
    
}
